package com.example.baiducrabs.bayifu.screenrecorder;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Environment;
import android.util.Log;

import com.coremedia.iso.boxes.Container;
import com.googlecode.mp4parser.authoring.Movie;
import com.googlecode.mp4parser.authoring.Track;
import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder;
import com.googlecode.mp4parser.authoring.container.mp4.MovieCreator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2018/4/9.
 */

public class ScreenRecorderUtils {

    private Activity mContext;
    public MediaProjectionManager mMediaProjectionManager;
    private String path;
    private ScreenRecorder mRecorder;
    private MediaRecorder audrioRecorder;


    private volatile static ScreenRecorderUtils sruInstance=null;

    public ScreenRecorderUtils(Activity mContext) {
        this.mContext = mContext;
        mMediaProjectionManager = (MediaProjectionManager) mContext.getSystemService(mContext.MEDIA_PROJECTION_SERVICE);

    }

    public  static  ScreenRecorderUtils getInstance(Activity mContext){
            if(sruInstance==null)
            {
                synchronized (ScreenRecorderUtils.class){
                    if(sruInstance==null){
                        sruInstance=new ScreenRecorderUtils(mContext);
                    }
                }
            }
        return sruInstance;
    }

    public void startRecording(int resultCode, Intent data){
        MediaProjection mediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data);
        if (mediaProjection == null) {
            Log.e("@@", "media projection is null");
            return;
        }
        // video size
        final int width = 1080;
        final int height =1920 ;
        path = Environment.getExternalStorageDirectory()+ File.separator+
                "record-" + width + "x" + height;
        recordVideo(mediaProjection, width, height);//录屏

        try {
            recordAudio();//录音
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void onClose(){
        if(mRecorder != null){
            mRecorder.quit();
            mRecorder = null;
        }
    }

    public void stopRecording(){
        if (mRecorder != null) {
            //关闭视频数据
            mRecorder.quit();
            mRecorder = null;
            //关闭音频数据
            audrioRecorder.stop();
            audrioRecorder.release();
            audrioRecorder = null;
        }
    }

    public void SynthesisRecording(){
        //音视频合成
        try {
            saveRecord();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * Description:录屏
     * Data: 2018/4/9 20:38
     * @author: lxy
     */
    private void recordVideo(MediaProjection mediaProjection, int width, int height) {

        File file = new File(path + ".mp4");
        final int bitrate = 6000000;
        mRecorder = new ScreenRecorder(width, height, bitrate, 1, mediaProjection, file.getAbsolutePath());
        mRecorder.start();
//        mContext.moveTaskToBack(true);
    }
    
    /**
     * Description:录音
     * Data: 2018/4/9 20:38
     * @author: lxy
     */
    private void recordAudio() throws IOException {
        audrioRecorder = new MediaRecorder();

//设置音频来源
        audrioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

//设置音频输出格式
        audrioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

//设置录音编码格式
        audrioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

//设置输出文件名
        audrioRecorder.setOutputFile(path+".mp3");

//录制开始

        audrioRecorder.prepare();
        audrioRecorder.start();
    }

    /**
     * Description:合成
     * Data: 2018/4/9 20:38
     * @author: lxy
     */
    private void saveRecord() throws IOException {
        Movie countVideo = MovieCreator.build(path+".mp4");

        Movie countAudioEnglish = MovieCreator.build(path+".mp3");

        Track audioTrackEnglish = countAudioEnglish.getTracks().get(0);

        countVideo.addTrack(audioTrackEnglish);

        {

            Container out = new DefaultMp4Builder().build(countVideo);

            FileOutputStream fos = new FileOutputStream(new File(path+"save.mp4"));

            out.writeContainer(fos.getChannel());

            fos.close();

        }
    }
}
